<?php
namespace app\admin\controller;
use app\admin\controller\Base;//权限验证
use think\Loader;
use think\Session;
use think\Db;
class Article extends Base{	
    public function index(){
		$list=Db::name('article')->alias('a')
		->join('cate b','a.cateid=b.id')
		->field('a.id,b.catename,a.title')
		->order('a.id','desc')
		->paginate(5);
		$page=$list->render();
		$this ->assign('page',$page);
		$this->assign('list',$list);		
		return $this->fetch('list');		
	}	
	public function edit(){		
		$id=input('id');
		$author=Session::get('adminname');
		$adminname=Session::get('adminname');
		$admin=db('admin')->where('username',$adminname)->column('id');     
		$adminid=$admin[0];
		$articles=db('article')->find($id);
		$time = date("Y-m-d H:i:s");
		if(request()->isPost()){
			$data=[
				'title'=>input('title'),
				'duction'=>input('duction'),
				'des'=>input('des'),
				'keywords'=>str_replace('，', ',', input('keywords')),
				'content'=>input('content'),
				'author'=>$author,
				'cateid'=>input('cateid'),
				'aid'=>$adminid,
				'time'=>$time,
			];
			if(input('state')=='on'){
                $data['state']=1;
            }
            if($_FILES['pic']['tmp_name']){
                $file = request()->file('pic');
                $info = $file->move(ROOT_PATH . 'public' . DS . 'static/uploads');
                $data['pic']='/uploads/'.$info->getSaveName();
			}
			$validate = \think\Loader::validate('Article');
			if(db('Article')->where('id',$id)->update($data)){
				return $this->success('修改文章成功！','Article/index');
			}else{
				return $this->error('修改文章失败！');
			}
			return;
		}
		$cateres=db('cate')->select();
        $this->assign('cateres',$cateres);
		$this->assign('articles',$articles);
		return $this->fetch('article/edit');
	}
	public function add(){    
		//发布文章	
    	if(request()->isPost()){
			// dump($_POST); die;
			$author=Session::get('adminname');
			$adminname=Session::get('adminname');
			$admin=db('admin')->where('username',$adminname)->column('id');
        	$adminid=$admin[0];
			$time = date("Y-m-d H:i:s");
			$data=[
				'title'=>input('title'),
				'duction'=>input('duction'),
                'des'=>input('des'),
                'keywords'=>str_replace('，', ',', input('keywords')),
				'content'=>input('content'),
				'author'=>$author,
				'cateid'=>input('cateid'),
				'aid'=>$adminid,
    			'time'=>$time,
            ];
            if(input('state')=='on'){
                $data['state']=1;
            }
            if($_FILES['pic']['tmp_name']){
                $file = request()->file('pic');
                $info = $file->move(ROOT_PATH . 'public' . DS . 'static/uploads');
                $data['pic']='/uploads/'.$info->getSaveName();
            }
    		$validate = \think\Loader::validate('Article');
    		if(!$validate->scene('add')->check($data)){
			   $this->error($validate->getError()); die;
			}
    		if(db('Article')->insert($data)){
    			return $this->success('添加文章成功！','Article/index');
    		}else{
    			return $this->error('添加文章失败！');
    		}
    		return;
    	}
    	$author=db('admin')->select();
    	$this->assign('author',$author);
        $cateres=db('cate')->select();
        $this->assign('cateres',$cateres);
        return $this->fetch();
	} 
	public function del(){
		$id=input('id');
		if(db('Article')->delete(input('id'))){
			$this->success('删除文章成功！','index');
		}else{
			$this->error('删除文章失败！');
		}
	}
}              